package com.siyao.redvip.components.imagepicker.adapter;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.siyao.redvip.R;

import java.util.ArrayList;
import java.util.List;

import uk.co.senab.photoview.PhotoView;

public class ImageBrowserAdapter extends PagerAdapter {

    private List<String> mPhotos = new ArrayList<String>();
    private Context context;
    public DisplayImageOptions options;

    public interface OnItemClickCallback {
        void onItemClick();
    }

    private OnItemClickCallback onItemClickCallback;

    public ImageBrowserAdapter(Context context, List<String> photos) {
        mPhotos = photos;
        this.context = context;
        options = new DisplayImageOptions.Builder().showImageOnLoading(R.drawable.ic_launcher).showImageForEmptyUri(R.drawable.ic_launcher)
                .showImageOnFail(R.drawable.ic_launcher).cacheInMemory(true).cacheOnDisc(true).considerExifParams(true)
                .displayer(new RoundedBitmapDisplayer(20)).build();
    }

    @Override
    public int getCount() {
        return mPhotos.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == (View) object;
    }

    @Override
    public View instantiateItem(ViewGroup container, int position) {
        View view = LayoutInflater.from(context).inflate(R.layout.layout_photoview, null);
        PhotoView photoView = (PhotoView) view.findViewById(R.id.pv);
        ImageLoader.getInstance().displayImage(mPhotos.get(position), photoView);
        container.addView(view, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        photoView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                if (onItemClickCallback != null) {
                    onItemClickCallback.onItemClick();
                }
            }
        });
        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    public void setDataChanged(List<String> photos) {
        this.mPhotos = photos;
        this.notifyDataSetChanged();
    }

    public void setOnItemClickCallback(OnItemClickCallback onItemClickCallback) {
        this.onItemClickCallback = onItemClickCallback;
    }
}
